package com.ymatou.iapi.thirdpartysync.testcase;

import java.util.ArrayList;
import java.util.List;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.google.gson.JsonObject;
import com.ymatou.iapi.productmanager.parameter.product.AddSellerProductRequest;
import com.ymatou.iapi.productmanager.parameter.product.ModifySellerProductRequest;
import com.ymatou.iapi.productmanager.service.ModifySellerProductCall;
import com.ymatou.iapi.thirdpartysync.parameter.addProductBean;
import com.ymatou.iapi.thirdpartysync.parameter.productCommandSyncBean;
import com.ymatou.iapi.thirdpartysync.service.productCommandSyncCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.ProductManagerCallServiceV2;
import com.ymttest.business.service.ProductManagerCallServiceV3;
import com.ymttest.business.service.ThirdpartysyncCallService;
import com.ymttest.database.model.thirdpartysync.fenqileproduct;
import com.ymttest.database.model.thirdpartysync.fenqileproductExample;
import com.ymttest.database.model.thirdpartysync.fenqilesku;
import com.ymttest.database.model.thirdpartysync.fenqileskuExample;
import com.ymttest.database.sqlwapper.thirdpartysync.thirdpartysyncWapper;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.TestDataManager;
/**
*商品指令同步
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_productCommandSync{
	private static productCommandSyncBean bean;
	private static productCommandSyncCall call;
	addProductBean addbean=new addProductBean();
	fenqileproductExample productExample=new fenqileproductExample();
	thirdpartysyncWapper syncWapper=new thirdpartysyncWapper(); 
	fenqilesku sku=new fenqilesku();
	fenqileskuExample skuExample=new fenqileskuExample();
	int sellerId = Integer.parseInt(EnvSetup.getData("autotest_seller1"));
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("商品指令同步");
	}

	@Before
	public void caseUp() {
		bean=new productCommandSyncBean();
		call=new productCommandSyncCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(call.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_productCommandSync_001() {
		//删除，fenqileproduct 变为下架，action=-1 无效
		Logger.start(true,"正例-商品指令同步-删除");
		try {
			Logger.comment("添加商品并同步到分期乐");
			addProductBean addbean=new addProductBean();
			 int sellerId = Integer.parseInt(EnvSetup.getData("autotest_seller1"));
			//创建商品
			AddSellerProductRequest addpro = ProductManagerCallServiceV2.initAddSellerProductRequest(sellerId);
			addpro.getProduct().getCatalogList().remove(0);
			//addpro.getProduct().setSecondCategoryId(768);
			addpro.getProduct().setThirdCategoryId(1003);
			addpro.getProduct().setBrandId(ProductManagerCallServiceV3.BrandId);
			
			String productId = ProductManagerCallServiceV2.addNewProduct(addpro).get("Data").getAsJsonObject().get("ProductId").getAsString();
			
			List<String> ids=new ArrayList();
			ids.add(productId);
			addbean.setProductIdList(ids);
			ThirdpartysyncCallService.AddProductCall(addbean, productId);
			productExample.createCriteria().andActionEqualTo(0).andSyncStatusEqualTo(1).andYmtSaleStatusEqualTo(1).andYmtProductIdEqualTo(productId);
			List<fenqileproduct> prodcuts_DB=syncWapper.selectByExample(productExample);
			int times=0;
			do{
			 	 if(prodcuts_DB.size()==0 )
				 {
			 	    times+=2000;
					Thread.sleep(times);
					prodcuts_DB=syncWapper.selectByExample(productExample);
					Logger.comment("查询数据库休眠的时间："+times);
					if(prodcuts_DB.size()==0) continue;
				 }else	 break;
		    }while(times<=10000);
			if(times>10000)
				Logger.comment("商品同步到分期乐异常");
			
			Logger.comment("修改商品价格");
			//修改商品价格
			addpro.getProduct().getCatalogList().get(0).setPrice(30);
			addpro.getProduct().getCatalogList().get(0).setVipPrice(29);
			addpro.getProduct().getCatalogList().get(0).setNewPrice(28);
			
			JsonObject deleteObject=ProductManagerCallServiceV2.batchDeleteProducts(ids, sellerId);

			
			Thread.sleep(10000);
			bean.setActionType("productCommandSync");
			bean.setActivityId(0);
			bean.setProductId(productId);
			bean.setTransactionId(TestDataManager.getRandomIntNum(9));
			call.setData(bean);
			call.callService();			
			Thread.sleep(10000);
			
			
			productExample.clear();
			productExample.createCriteria().andSyncStatusEqualTo(1).andYmtProductIdEqualTo(productId);
			List<fenqileproduct>  newprodcuts_DB=syncWapper.selectByExample(productExample);
			int newTimes=2000;
			do{
				if(newprodcuts_DB.get(0).getUpdateTime()==prodcuts_DB.get(0).getUpdateTime())
				{
					
					Thread.sleep(newTimes);
					productExample.clear();
					productExample.createCriteria().andSyncStatusEqualTo(1).andYmtProductIdEqualTo(productId);
					newprodcuts_DB=syncWapper.selectByExample(productExample);
					Logger.comment("修改同步 查询数据库休眠的时间："+newTimes);
					newTimes+=2000;
					continue;
				}else {
					break;
				}
			}while(newTimes<10000);
			if(newTimes>10000)
				Logger.comment("商品同步到分期乐异常");
			
			Logger.verifyEquals(2, newprodcuts_DB.get(0).getYmtSaleStatus(), "验证FenqileProduct 表中的YmtSaleStatus=2下架");
			Logger.verifyEquals(-1, newprodcuts_DB.get(0).getAction(), "验证fenqilFenqileProduct esku表中的Action=-1删除");
			
			skuExample.clear();
			skuExample.createCriteria().andSyncStatusEqualTo(1).andYmtProductIdEqualTo(productId);
			List<fenqilesku> skus_DB=syncWapper.selectByExample(skuExample);
			Logger.verifyEquals(-1, skus_DB.get(0).getAction(), "验证fenqilesku表中的Action=-1删除");
			Logger.verifyEquals("20", skus_DB.get(0).getYmtPrice().stripTrailingZeros().toPlainString(), "验证fenqilesku表中的ymtPrice");
			Logger.verifyEquals("22.89", skus_DB.get(0).getFqlPrice().stripTrailingZeros().toPlainString(), "验证fenqilesku表中的FqlPrice");	
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_productCommandSync_002() {
		//针对扣库存 直接调productCommandSync，productCommandSync内部调CatalogStockChange指令
		Logger.start(true,"正例-商品指令同步-库存更改");
		try {
			Logger.comment("添加商品并同步到分期乐");
			 int sellerId = Integer.parseInt(EnvSetup.getData("autotest_seller1"));
			//创建商品
			AddSellerProductRequest addpro = ProductManagerCallServiceV2.initAddSellerProductRequest(sellerId);
			addpro.getProduct().getCatalogList().remove(0);
			//addpro.getProduct().setSecondCategoryId(768);
			addpro.getProduct().setThirdCategoryId(1003);
			addpro.getProduct().setBrandId(ProductManagerCallServiceV3.BrandId);
			
			String productId = ProductManagerCallServiceV2.addNewProduct(addpro).get("Data").getAsJsonObject().get("ProductId").getAsString();
			
			List<String> ids=new ArrayList();
			ids.add(productId);
			addbean.setProductIdList(ids);
			ThirdpartysyncCallService.AddProductCall(addbean, productId);
			productExample.createCriteria().andActionEqualTo(0).andSyncStatusEqualTo(1).andYmtSaleStatusEqualTo(1).andYmtProductIdEqualTo(productId);
			List<fenqileproduct> prodcuts_DB=syncWapper.selectByExample(productExample);
			
			skuExample.createCriteria().andActionEqualTo(0).andSyncStatusEqualTo(1).andYmtProductIdEqualTo(productId);
			List<fenqilesku> skus_DB=syncWapper.selectByExample(skuExample);
			int times=0;
			do{
			 	 if(prodcuts_DB.size()==0 )
				 {
			 	    times+=2000;
					Thread.sleep(times);
					prodcuts_DB=syncWapper.selectByExample(productExample);
					skus_DB=syncWapper.selectByExample(skuExample);
					Logger.comment("查询数据库休眠的时间："+times);
					if(prodcuts_DB.size()==0) continue;
				 }else	 break;
		    }while(times<=10000);
			if(times>10000)
				Logger.comment("商品同步到分期乐异常");
			
			Logger.comment("修改商品库存");
			//修改商品库存
			addpro.getProduct().getCatalogList().get(0).setStock(2000);
			
			ModifySellerProductRequest modifyBean=ProductManagerCallServiceV2.getModifySellerProductRequestFromAddProductBean(productId,addpro);
			ModifySellerProductCall modifySellerProductCall=new ModifySellerProductCall();
			modifySellerProductCall.setData(modifyBean);
			modifySellerProductCall.callService();
			
			Thread.sleep(10000);
			bean.setActionType("CatalogStockChange");
			bean.setActivityId(0);
			bean.setProductId(productId);
			bean.setTransactionId(TestDataManager.getRandomIntNum(9));
			call.setData(bean);
			call.callService();			
			Thread.sleep(10000);
			
			skuExample.clear();
			skuExample.createCriteria().andSyncStatusEqualTo(1).andYmtProductIdEqualTo(productId);
			List<fenqilesku> newskus_DB=syncWapper.selectByExample(skuExample);
			int newTimes=0;
			do{
				if(newskus_DB.get(0).getUpdateTime()==skus_DB.get(0).getUpdateTime())
				{
					newTimes+=2000;
					Thread.sleep(newTimes);
					skuExample.clear();
					skuExample.createCriteria().andSyncStatusEqualTo(1).andYmtProductIdEqualTo(productId);
					newskus_DB=syncWapper.selectByExample(skuExample);
					Logger.comment("修改同步 查询数据库休眠的时间："+newTimes);
					continue;
				}else {
					break;
				}
			}while(newTimes<10000);
			if(newTimes>10000)
				Logger.comment("商品同步到分期乐异常");
			else 
				Logger.verifyEquals("2000", newskus_DB.get(0).getYmtStock(), "验证FenqileProduct 表中的YmtStock");
			
			
//			Logger.verifyEquals(2, newprodcuts_DB.get(0).getYmtSaleStatus(), "验证FenqileProduct 表中的YmtSaleStatus=2下架");
//			Logger.verifyEquals(-1, newprodcuts_DB.get(0).getAction(), "验证fenqilFenqileProduct esku表中的Action=-1删除");
//			
//			skuExample.clear();
//			skuExample.createCriteria().andSyncStatusEqualTo(1).andYmtProductIdEqualTo(productId);
//			List<fenqilesku> skus_DB=syncWapper.selectByExample(skuExample);
//			Logger.verifyEquals(-1, skus_DB.get(0).getAction(), "验证fenqilesku表中的Action=-1删除");
//			Logger.verifyEquals("20", skus_DB.get(0).getYmtPrice().stripTrailingZeros().toPlainString(), "验证fenqilesku表中的ymtPrice");
//			Logger.verifyEquals("22.89", skus_DB.get(0).getFqlPrice().stripTrailingZeros().toPlainString(), "验证fenqilesku表中的FqlPrice");	
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}